<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // async-await也是一种解决异步回调地狱的方案 await必须要搭配async使用
    // 生名函数为异步函数
    async function getList(){
        return "成功啦"
    }
    getList().then(res=>{
        console.log(res)
    })

    function getNum(){
        return new Promise((reslove,reject)=>{
        setTimeout(()=>{
            console.log(124345)
            reslove(1234)
        },2000)
    })
    }
    async function getOne(){
        // await 等待的意思 当awit后面为promise对象时需要等待promise状态发生改变后才会继续执行
        let res = await getNum();
        console.log("2222")
    }
    getOne();
    function sendHttp(url,method){
         return new Promise((reslove,reject)=>{
            let xhr = new XMLHttpRequest();
            xhr.open(method,url);
            xhr.send();
            xhr.onreadystatechange=()=>{
                if(xhr.readyState===4){
                    let res = JSON.parse(xhr.response);
                    reslove(res)
                }
            }
         })
    }
    // 异步代码同步体现
    let getBannerList =async ()=>{
        let res = await sendHttp("https://apif.java.crmeb.net/api/front/index",'get');
        console.log(res)
        let res1 = await sendHttp('https://apif.java.crmeb.net/api/front/product/leaderboard','get');
        console.log(res1)
    }
    getBannerList();
</script>
</html>